package org.pet.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.pet.entity.PetScience;
import org.pet.mapper.PetScienceMapper;
import org.pet.service.PetScienceService;
import org.pet.service.PetVarietyService;
import org.pet.util.PageBean;
import org.pet.util.Result;
import org.pet.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class PetScienceServiceImpl implements PetScienceService {

    @Autowired
    private PetScienceMapper petScienceMapper;
    @Autowired
    private PetVarietyService petVarietyService;

    private static final Integer ID = 1;//管理员id

    @Override
    public Result info(Integer id) {
        PetScience petScience = petScienceMapper.info(id);
        if (petScience == null) //若找不到文章
            return Result.error("文章不存在");
        return Result.success(petScience);
    }

    @Override
    public PageBean<PetScience> listPage(Integer pageNum, Integer pageSize, String petVariety, String state) {
        //创建PageBean对象
        PageBean<PetScience> list = new PageBean<>();
        //开启分页查询 PageHelper , 会自动给查询后面加上limit
        PageHelper.startPage(pageNum,pageSize);
        //获取list
        List<PetScience> lists = petScienceMapper.list(petVariety,state);
        //将返回值转换未page方便获取，返回的分页总数和返回数据列
        Page<PetScience> page = (Page<PetScience>) lists;

        list.setTotal(page.getTotal());
        list.setItems(page.getResult());
        return list;
    }

    //新增
    @Override
    public Result add(PetScience petScience) {
        //获取用户信息
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        String name = (String) claims.get("name");
        petScience.setUserId(id);
        petScience.setClicks(0);
        petScience.setUserName(name);
        //是否有一级宠物种类标签
        if (petScience.getPetVariety() == null || petScience.getPetVariety().isEmpty()){
            //是否有二级宠物种类标签
            if (petScience.getPetSubVariety() == null || petScience.getPetSubVariety().isEmpty()) {
                return Result.error("请提供文章分区");
            }
            String variety = petVarietyService.getVariety(petScience.getPetSubVariety());
            if (variety == null || variety.isEmpty()){
                return Result.error("分区不存在");
            }
            petScience.setPetVariety(variety);
        }
        petScience.setScore(0.0);//设置初始评分为0
        petScience.setRemove(0);//设置删除状态为未删除
        return petScienceMapper.insert(petScience) == 0 ? Result.error("操作失败") : Result.success();
    }
    //更新
    @Override
    public Result update(PetScience petScience) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        PetScience oldPetScience = petScienceMapper.info(petScience.getId());
        //若文章不是当前用户创建，且当前用户不是系统管理员
        if (!Objects.equals(id, oldPetScience.getUserId()) && !Objects.equals(id, ID)){
            return Result.error("没有权限");
        }
        //若更新了二级宠物种类，则判断是否需要更新一级宠物种类
        if (petScience.getPetSubVariety() != null){
            String newPetVariety = petVarietyService.getVariety(petScience.getPetSubVariety());
            String oldPetVariety = oldPetScience.getPetVariety();
            if (!newPetVariety.equals(oldPetVariety)) {
                petScience.setPetVariety(newPetVariety);
            }
        }
        return petScienceMapper.update(petScience) == 0 ? Result.error("操作失败") : Result.success();
    }

    //删除
    @Override
    public Result delete(Integer id) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer loginId = (Integer) claims.get("id");
        if (Objects.equals(loginId,ID)) {//系统管理员可以直接删除
            return petScienceMapper.delete(id) == 0 ? Result.error("操作失败") : Result.success();
        }
        PetScience petScience = petScienceMapper.info(id);
        if (!Objects.equals(petScience.getUserId(),loginId)) {//判断是否是当前用户是否有权限
            return Result.error("无权限");
        }
        return petScienceMapper.delete(id) == 0 ? Result.error("操作失败") : Result.success();
    }

    @Override
    public Result updateClick(Integer id) {
        return petScienceMapper.updateClick(id) == 0 ? Result.error("操作失败") : Result.success();
    }
}
